<HTML>
<HEAD>
<TITLE>[Chapter 9] 9.4 CheckboxGroup</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:44:39 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch09_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 9<br>Pick Me</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch09_05.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-9-SECT-4">9.4 CheckboxGroup</A></h2>

<P CLASS=para>
<A NAME="CH09.CHECK3"></A><A NAME="CH09.CHECK4"></A>The <tt CLASS=literal>CheckboxGroup</tt> lets multiple 
checkboxes work together to provide a mutually exclusion choice (at most 
one <tt CLASS=literal>Checkbox</tt> can be selected 
at a time). Because the <tt CLASS=literal>CheckboxGroup</tt> 
is neither a <tt CLASS=literal>Component</tt> nor a 
<tt CLASS=literal>Container</tt>, you should normally 
put all the <tt CLASS=literal>Checkbox</tt> components 
associated with a <tt CLASS=literal>CheckboxGroup</tt> 
in their own <tt CLASS=literal>Panel</tt> (or other 
<tt CLASS=literal>Container</tt>). The <tt CLASS=literal>LayoutManager</tt> 
of the <tt CLASS=literal>Panel</tt> should be <tt CLASS=literal>GridLayout 
(0, 1)</tt> if you want them in one column. <A HREF="ch09_04.htm#JAWT-CH-9-FIG-5">Figure 9.5</A> shows both a good way and bad way of positioning 
a set of <tt CLASS=literal>Checkbox</tt> items in 
a <tt CLASS=literal>CheckboxGroup</tt>. The image 
on the left is preferred because the user can sense that the items are 
grouped; the image on the right suggests three levels of different checkboxes 
and can therefore surprise the user when checkboxes are deselected. 

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-9-FIG-5">Figure 9.5: Straightforward and confusing layouts of Checkbox components</A></h4>


<p>
<img align=middle src="./figs/jawt0905.gif" alt="[Graphic: Figure 9-5]" width=503 height=211 border=0>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-9-SECT-4.1">CheckboxGroup Methods</A></h3>Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public CheckboxGroup () </I><br>
<DD>

<P CLASS=para>
This constructor creates an instance of <tt CLASS=literal>CheckboxGroup</tt>. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getSelectedCheckbox () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br><I CLASS=emphasis>public Checkbox getCurrent () <img src="gifs/wstar.gif" alt="(Deprecated)" border=0></I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getSelectedCheckbox()</tt> method 
returns the <tt CLASS=literal>Checkbox</tt> within 
the <tt CLASS=literal>CheckboxGroup</tt> whose value 
is <tt CLASS=literal>true</tt>. If no item is selected, 
<tt CLASS=literal>null</tt> is returned. 

<P CLASS=para>
<tt CLASS=literal>getCurrent()</tt> is the Java 1.0 
name for this method. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void setSelectedCheckbox (Checkbox checkbox) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br><I CLASS=emphasis>public synchronized void setCurrent (Checkbox checkbox) <img src="gifs/wstar.gif" alt="(Deprecated)" border=0></I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setSelectedCheckbox()</tt> method 
makes <tt CLASS=literal>checkbox</tt> the currently 
selected <tt CLASS=literal>Checkbox</tt> within the 
<tt CLASS=literal>CheckboxGroup</tt>. If <tt CLASS=literal>checkbox</tt>is <tt CLASS=literal>null</tt>, 
the method deselects all the items in the <tt CLASS=literal>CheckboxGroup</tt>. 
If <tt CLASS=literal>checkbox</tt> is not within the 
<tt CLASS=literal>CheckboxGroup</tt>, nothing happens. 

<P CLASS=para>
<tt CLASS=literal>setCurrent()</tt> is the Java 1.0 
name for this method. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public String toString () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>CheckboxGroup</tt> 
creates a <tt CLASS=literal>String</tt> representation 
of the current choice (as returned by <tt CLASS=literal>getSelectedCheckbox()</tt>). 
Using the "straightforward" layout in <A HREF="ch09_04.htm#JAWT-CH-9-FIG-5">Figure 9.5</A> as an example, 
the results would be: 

<DIV CLASS=screen>
<P>
<PRE>
java.awt.CheckboxGroup[current=java.awt.Checkbox[0,31,85x21,
    label=Helvetica,state=true]]
</PRE>
</DIV>

<P CLASS=para>
If there is no currently selected item, the results within the square brackets 
would be <tt CLASS=literal>current=null</tt>. </DL>
</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch09_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch09_05.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Checkbox</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>ItemSelectable</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
